/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//Xiong@2011.11.22: merge

package com.android.launcherplus;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class DotsIndicator extends ViewGroup {
	private View mIndicator;
	private static final String TAG = "DotsIndicator";
		
	private int mItems=7;
	private int mCurrent=0;
 
	private int mIndicatorHeight;
	private int mBtnbarHeight;	 
	private int mOrientation; 

	public DotsIndicator(Context context) {
		super(context);
		initIndicator(context);
	}

	public DotsIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);		 
		mOrientation = getResources().getConfiguration().orientation; 
		initIndicator(context);
	}

	public DotsIndicator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);		
		mOrientation = getResources().getConfiguration().orientation; 
		initIndicator(context);
	}
	private void initIndicator(Context context){		
		mIndicator=new PreviewPager(context);
		((PreviewPager) mIndicator).setTotalItems(mItems);
		((PreviewPager) mIndicator).setCurrentItem(mCurrent);		
		((PreviewPager) mIndicator).setDirection(1); 		
		addView(mIndicator); 
		mIndicatorHeight=getContext().getResources().getDimensionPixelSize(R.dimen.paginator_height);
		mBtnbarHeight=getContext().getResources().getDimensionPixelSize(R.dimen.button_bar_height);		 
	}
	public void setItems(int items){
		mItems=items;
	    ((PreviewPager) mIndicator).setTotalItems(mItems);
	    ((PreviewPager) mIndicator).setCurrentItem(mCurrent);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int realHeight=mIndicatorHeight; 
	    mIndicator.measure(getWidth(), realHeight); 
	    int realHeightMeasurespec=MeasureSpec.makeMeasureSpec(realHeight, MeasureSpec.EXACTLY);
		super.onMeasure(widthMeasureSpec, realHeightMeasurespec);			
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		LinearLayout.LayoutParams params; 
        params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mIndicator.measure(getWidth(), mIndicatorHeight); 
        mIndicator.setLayoutParams(params);
        mIndicator.layout(0, 0, getWidth(), mIndicatorHeight); 
	}
	public void setPosition(float percent){	
		int position=Math.round(mItems*percent);
		((PreviewPager) mIndicator).setCurrentItem(position);
		mCurrent=position;
	}
	public void fullIndicate(int position){
		setVisibility(View.VISIBLE);
		((PreviewPager) mIndicator).setCurrentItem(position);
        mCurrent = position;
	}

	public void adjustGravity() {
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(this.getLayoutParams());			
	    lp.gravity = Gravity.BOTTOM; 		
		if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
		    lp.bottomMargin = 0;  
		} else {
		    lp.bottomMargin = mBtnbarHeight;  
		}
	    
		setLayoutParams(lp);
		removeView(mIndicator);
		initIndicator(getContext());
	}
}
